import VueRouter from 'vue-router';
// console.dir(VueRouter);
//先保存原型上的push函数，具有跳转页面功能 push可以传三个参数，push返回的是promise
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
// console.log(originPush);

//重写push方法,此时的push函数，指向$router
VueRouter.prototype.push = function (path, onSucess, onFailure) {
  //在调用原先push函数，此时是普通调用，this指向window，只有指回原先的this，才具有跳转功能
  //onSucess 和onFailure 是成功和失败的回调函数，传入任一回调函数都能满足其要求，捕获错误，
  if (onSucess || onFailure) {
    originPush.call(this, path, onSucess, onFailure);
  } else {
    //如果没有传入就自动，就自动添加进去
    originPush.call(
      this,
      path,
      () => {},
      () => {}
    );
  }
};

//重写replace方法，this.$router.replace(path) 不保留原始记录
VueRouter.prototype.replace = function (
  path,
  onSucess = () => {},
  onFailure = () => {}
) {
  //没传成功和失败的回调就使用默认值
  originReplace.call(this, path, onSucess, onFailure);
};
